﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:T_BaseTheorySubject
	/// </summary>
	public partial class T_BaseTheorySubject
	{
		public T_BaseTheorySubject()
		{}
		#region  BasicMethod



		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Model.T_BaseTheorySubject model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_BaseTheorySubject(");
			strSql.Append("Code,Title,TypeCode,Answer,AnswerAnalysis,ClassificationCode,ClassificationName,Knowledge,LevelOfDifficultyCode,LevelOfDifficulty,Tag,CreatDate,md5val)");
			strSql.Append(" values (");
			strSql.Append("@Code,@Title,@TypeCode,@Answer,@AnswerAnalysis,@ClassificationCode,@ClassificationName,@Knowledge,@LevelOfDifficultyCode,@LevelOfDifficulty,@Tag,@CreatDate,@md5val)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.NVarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,500),
					new SqlParameter("@TypeCode", SqlDbType.Int,4),
					new SqlParameter("@Answer", SqlDbType.NVarChar,500),
					new SqlParameter("@AnswerAnalysis", SqlDbType.NVarChar,500),
					new SqlParameter("@ClassificationCode", SqlDbType.Int,4),
					new SqlParameter("@ClassificationName", SqlDbType.NVarChar,50),
					new SqlParameter("@Knowledge", SqlDbType.NVarChar,50),
					new SqlParameter("@LevelOfDifficultyCode", SqlDbType.Int,4),
					new SqlParameter("@LevelOfDifficulty", SqlDbType.NChar,10),
					new SqlParameter("@Tag", SqlDbType.NVarChar,50),
					new SqlParameter("@CreatDate", SqlDbType.NChar,10),
					new SqlParameter("@md5val", SqlDbType.NVarChar,50)};
			parameters[0].Value = model.Code;
			parameters[1].Value = model.Title;
			parameters[2].Value = model.TypeCode;
			parameters[3].Value = model.Answer;
			parameters[4].Value = model.AnswerAnalysis;
			parameters[5].Value = model.ClassificationCode;
			parameters[6].Value = model.ClassificationName;
			parameters[7].Value = model.Knowledge;
			parameters[8].Value = model.LevelOfDifficultyCode;
			parameters[9].Value = model.LevelOfDifficulty;
			parameters[10].Value = model.Tag;
			parameters[11].Value = model.CreatDate;
			parameters[12].Value = model.md5val;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.T_BaseTheorySubject model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_BaseTheorySubject set ");
			strSql.Append("Code=@Code,");
			strSql.Append("Title=@Title,");
			strSql.Append("TypeCode=@TypeCode,");
			strSql.Append("Answer=@Answer,");
			strSql.Append("AnswerAnalysis=@AnswerAnalysis,");
			strSql.Append("ClassificationCode=@ClassificationCode,");
			strSql.Append("ClassificationName=@ClassificationName,");
			strSql.Append("Knowledge=@Knowledge,");
			strSql.Append("LevelOfDifficultyCode=@LevelOfDifficultyCode,");
			strSql.Append("LevelOfDifficulty=@LevelOfDifficulty,");
			strSql.Append("Tag=@Tag,");
			strSql.Append("CreatDate=@CreatDate,");
			strSql.Append("md5val=@md5val");
			strSql.Append(" where TheorySubjectId=@TheorySubjectId");
			SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.NVarChar,50),
					new SqlParameter("@Title", SqlDbType.NVarChar,500),
					new SqlParameter("@TypeCode", SqlDbType.Int,4),
					new SqlParameter("@Answer", SqlDbType.NVarChar,500),
					new SqlParameter("@AnswerAnalysis", SqlDbType.NVarChar,500),
					new SqlParameter("@ClassificationCode", SqlDbType.Int,4),
					new SqlParameter("@ClassificationName", SqlDbType.NVarChar,50),
					new SqlParameter("@Knowledge", SqlDbType.NVarChar,50),
					new SqlParameter("@LevelOfDifficultyCode", SqlDbType.Int,4),
					new SqlParameter("@LevelOfDifficulty", SqlDbType.NChar,10),
					new SqlParameter("@Tag", SqlDbType.NVarChar,50),
					new SqlParameter("@CreatDate", SqlDbType.NChar,10),
					new SqlParameter("@md5val", SqlDbType.NVarChar,50),
					new SqlParameter("@TheorySubjectId", SqlDbType.Int,4)};
			parameters[0].Value = model.Code;
			parameters[1].Value = model.Title;
			parameters[2].Value = model.TypeCode;
			parameters[3].Value = model.Answer;
			parameters[4].Value = model.AnswerAnalysis;
			parameters[5].Value = model.ClassificationCode;
			parameters[6].Value = model.ClassificationName;
			parameters[7].Value = model.Knowledge;
			parameters[8].Value = model.LevelOfDifficultyCode;
			parameters[9].Value = model.LevelOfDifficulty;
			parameters[10].Value = model.Tag;
			parameters[11].Value = model.CreatDate;
			parameters[12].Value = model.md5val;
			parameters[13].Value = model.TheorySubjectId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int TheorySubjectId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_BaseTheorySubject ");
			strSql.Append(" where TheorySubjectId=@TheorySubjectId");
			SqlParameter[] parameters = {
					new SqlParameter("@TheorySubjectId", SqlDbType.Int,4)
			};
			parameters[0].Value = TheorySubjectId;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string TheorySubjectIdlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_BaseTheorySubject ");
			strSql.Append(" where TheorySubjectId in ("+TheorySubjectIdlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.T_BaseTheorySubject GetModel(int TheorySubjectId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 TheorySubjectId,Code,Title,TypeCode,Answer,AnswerAnalysis,ClassificationCode,ClassificationName,Knowledge,LevelOfDifficultyCode,LevelOfDifficulty,Tag,CreatDate,md5val from T_BaseTheorySubject ");
			strSql.Append(" where TheorySubjectId=@TheorySubjectId");
			SqlParameter[] parameters = {
					new SqlParameter("@TheorySubjectId", SqlDbType.Int,4)
			};
			parameters[0].Value = TheorySubjectId;

			Model.T_BaseTheorySubject model=new Model.T_BaseTheorySubject();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.T_BaseTheorySubject DataRowToModel(DataRow row)
		{
			Model.T_BaseTheorySubject model=new Model.T_BaseTheorySubject();
			if (row != null)
			{
				if(row["TheorySubjectId"]!=null && row["TheorySubjectId"].ToString()!="")
				{
					model.TheorySubjectId=int.Parse(row["TheorySubjectId"].ToString());
				}
				if(row["Code"]!=null)
				{
					model.Code=row["Code"].ToString();
				}
				if(row["Title"]!=null)
				{
					model.Title=row["Title"].ToString();
				}
				if(row["TypeCode"]!=null && row["TypeCode"].ToString()!="")
				{
					model.TypeCode=int.Parse(row["TypeCode"].ToString());
				}
				if(row["Answer"]!=null)
				{
					model.Answer=row["Answer"].ToString();
				}
				if(row["AnswerAnalysis"]!=null)
				{
					model.AnswerAnalysis=row["AnswerAnalysis"].ToString();
				}
				if(row["ClassificationCode"]!=null && row["ClassificationCode"].ToString()!="")
				{
					model.ClassificationCode=int.Parse(row["ClassificationCode"].ToString());
				}
				if(row["ClassificationName"]!=null)
				{
					model.ClassificationName=row["ClassificationName"].ToString();
				}
				if(row["Knowledge"]!=null)
				{
					model.Knowledge=row["Knowledge"].ToString();
				}
				if(row["LevelOfDifficultyCode"]!=null && row["LevelOfDifficultyCode"].ToString()!="")
				{
					model.LevelOfDifficultyCode=int.Parse(row["LevelOfDifficultyCode"].ToString());
				}
				if(row["LevelOfDifficulty"]!=null)
				{
					model.LevelOfDifficulty=row["LevelOfDifficulty"].ToString();
				}
				if(row["Tag"]!=null)
				{
					model.Tag=row["Tag"].ToString();
				}
				if(row["CreatDate"]!=null)
				{
					model.CreatDate=row["CreatDate"].ToString();
				}
				if(row["md5val"]!=null)
				{
					model.md5val=row["md5val"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select TheorySubjectId,Code,Title,TypeCode,Answer,AnswerAnalysis,ClassificationCode,ClassificationName,Knowledge,LevelOfDifficultyCode,LevelOfDifficulty,Tag,CreatDate,md5val ");
			strSql.Append(" FROM T_BaseTheorySubject ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" TheorySubjectId,Code,Title,TypeCode,Answer,AnswerAnalysis,ClassificationCode,ClassificationName,Knowledge,LevelOfDifficultyCode,LevelOfDifficulty,Tag,CreatDate,md5val ");
			strSql.Append(" FROM T_BaseTheorySubject ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM T_BaseTheorySubject ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.TheorySubjectId desc");
			}
			strSql.Append(")AS Row, T.*  from T_BaseTheorySubject T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "T_BaseTheorySubject";
			parameters[1].Value = "TheorySubjectId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

